package com.resou.reader.reader.autopay;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.BookDetailBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.BookService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.mine.IView.UserInstance;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class AutoPayPresenter extends ResouBasePresenter<ContractAutoPay.AutoPayView> implements ContractAutoPay.AutoPayPresenter {

    BookService bookService;

    public AutoPayPresenter(ContractAutoPay.AutoPayView view) {
        super(view);
        bookService = ApiImp.getInstance().getService(BookService.class);
    }

    @Override
    public void quearAutoPayList(int page) {
        mView.showProgress();
        bookService.quearAutoPayList(UserInstance.getToken(), page, pageSize)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<List<BookDetailBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<List<BookDetailBean>> listResult) {
                        if (page == 0) {
                            mView.getSmartRefreshLayout().finishRefresh();
                            if (listResult.getData() == null || listResult.getData().size() == 0) {
                                mView.showEmpty();
//                                return;
                            }
                        }
                        if (listResult.getData().size() == pageSize) {
                            mView.getSmartRefreshLayout().finishLoadMore();
                            mView.showAutoPayList(listResult.getData());
                        } else {
                            mView.getSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                            mView.showAutoPayList(listResult.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }

    @Override
    public void cancellAutoPay(String bookIds) {
        mView.showProgress();
        bookService.cancellAutoPay(UserInstance.getToken(), bookIds)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<String> result) {
                        if (result.getCode() == 0) {
                            mView.cancellAutoSuccess();
                        } else {
                            mView.showError(new Throwable("取消订阅失败"));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(new Throwable("取消订阅失败"));
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }
}
